package com.kye.express.business.map;

import com.baidu.mapapi.map.BaiduMap;
import com.baidu.mapapi.map.MapStatusUpdateFactory;
import com.baidu.mapapi.model.LatLng;
import com.baidu.mapapi.model.LatLngBounds;

public class MapUtils {

    private static final int TIME_INTERVAL = 80;
    private static final double DISTANCE = 0.0001;

    public static void zoomToSpan(BaiduMap map, boolean animate, LatLng... latLngs) {
        LatLngBounds.Builder builder = new LatLngBounds.Builder();

        for (LatLng latLng : latLngs) builder = builder.include(latLng);

        if (animate) {
            map.animateMapStatus(MapStatusUpdateFactory.newLatLngBounds(builder.build()));
        } else {
            map.setMapStatus(MapStatusUpdateFactory.newLatLngBounds(builder.build()));
        }
    }

    public static void moveCenterPoint(BaiduMap map, boolean animate, LatLng... latLngs) {
        LatLngBounds.Builder builder = new LatLngBounds.Builder();

        for (LatLng latLng : latLngs) builder = builder.include(latLng);

        if (animate) {
            map.animateMapStatus(MapStatusUpdateFactory.newLatLngBounds(builder.build()));
        } else {
            map.setMapStatus(MapStatusUpdateFactory.newLatLngBounds(builder.build()));
        }
    }

    /**
     * 轨迹 转 地图
     */
    public static LatLng convertTrace2Map(com.baidu.trace.model.LatLng traceLatLng) {
        return new LatLng(traceLatLng.latitude, traceLatLng.longitude);
    }

    /**
     * 算斜率
     */
    public static double getSlope(LatLng fromPoint, LatLng toPoint) {
        if (toPoint.longitude == fromPoint.longitude) {
            return Double.MAX_VALUE;
        }
        double slope = ((toPoint.latitude - fromPoint.latitude) / (toPoint.longitude - fromPoint.longitude));
        return slope;
    }

    /**
     * 根据点和斜率算取截距
     */
    public static double getInterception(double slope, LatLng point) {
        double interception = point.latitude - slope * point.longitude;
        return interception;
    }


    /**
     * 计算x方向每次移动的距离
     */
    public static double getXMoveDistance(double slope) {
        if (slope == Double.MAX_VALUE) {
            return DISTANCE;
        }
        return Math.abs((DISTANCE * slope) / Math.sqrt(1 + slope * slope));
    }

    /**
     * 每一段在维度上应移动的距离
     */
    public static double getXMoveDistance(double slope, LatLng startPoint, LatLng nextPoint, int ci) {
        double xx = Math.pow((nextPoint.longitude - startPoint.longitude), 2) + Math.pow((nextPoint.latitude - startPoint.latitude), 2);
        double distance = Math.sqrt(xx);
        distance = distance / ci;
        if (slope == Double.MAX_VALUE) {
            return distance;
        }
        return Math.abs((distance * slope) / Math.sqrt(1 + slope * slope));
    }
}
